from blueprints import role_blueprint
from models import QueryOneInstance, Role, UpdateInstance
from tools.decorate import pack_session
from tools.response import ResponsePostJson, Response


@role_blueprint.route('/my/model/role/role',methods=['POST'])
@pack_session
def post_my_model_of_role_role(data):
    def cb(json):
        role = QueryOneInstance(Role,{'uid':data['session']['uid']})
        if role is None:
            role = Role(uid=data['session']['uid'])

        role.role=json['role']
        UpdateInstance(role)
        return Response("设置身份成功")
    return ResponsePostJson(['role'],cb)

@role_blueprint.route('/my/model/role/part',methods=['POST'])
@pack_session
def post_my_model_of_role_part(data):
    def cb(json):
        role = QueryOneInstance(Role,{'uid':data['session']['uid']})
        if role is None:
            role = Role(uid=data['session']['uid'])

        role.part=json['part']
        UpdateInstance(role)
        return Response("设置部门成功")
    return ResponsePostJson(['part'],cb)